<!--
[{id: '', goodsId: '', name: ''}]
-->
<template>
    <el-dialog title="选择淘宝官方活动" :visible.sync="showGoodsPop"  :close-on-click-modal="false">
        <el-form label-position="left"  style="width: 100%; padding-left: 20px;">
            <el-form-item label="所选淘宝官方活动：" v-if="count !== 1" >
                <div class="el-tag-box">
                    <el-tag v-for="(boxs, index) of boxCheck" :key="boxs.id" :closable="true" @close="delGoods(index)">{{boxs.activityName}}</el-tag>
                </div>
            </el-form-item>
        </el-form>
        <el-form ref="searchForm" class="search-block" :model="listQuery" label-position="left"  style="width: 100%">
            <!-- <input type="text" style="display: none"/> -->
            <el-row type="flex">
                <el-col :span="17">
                    <el-form-item label="淘宝官方活动名称：" prop="activityName" label-width="150px">
                        <el-input @keyup.enter.native="search" v-model="listQuery.activityName" placeholder="按淘宝官方活动名称搜索"  clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="1"></el-col>
                <el-col :span="6">
                    <el-button type="primary" size="mini" icon="el-icon-search" plain @click="search">搜索</el-button>
                </el-col>
            </el-row>
            <!-- <el-form-item label="" class="btn-item">
                
            </el-form-item> -->
        </el-form>
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border @row-click="clickRow">
            <el-table-column align="center" width="50">
                <template slot-scope="scope">
                    <label class="input-radio" v-if="count === 1">
                        <input type="radio" :value="{id: scope.row.id}" v-model="itemsRadio"   @change="(e) => setValue(e.target.checked, scope.row)" />
                        <span></span>
                    </label>
                    <label class="input-checkbox" v-else>
                        <!-- :disabled="isDisabled(scope.row.id)" -->
                        <input type="checkbox" :value="{id: scope.row.id}" v-model="itemsCheck"  @change="(e) => setValue(e.target.checked, scope.row)" />
                        <span></span>
                    </label>
                </template>
            </el-table-column>
            <el-table-column align="center" label="淘宝活动ID" prop="id"></el-table-column>
            <el-table-column align="center" label="淘宝活动名称" prop="activityName"></el-table-column>
            <el-table-column align="center" label="活动开始时间" prop="startTime"></el-table-column>
            <el-table-column align="center" label="活动结束时间" prop="endTime"></el-table-column>
            <el-table-column align="center" label="活动推广链接" width="200px">
                <template slot-scope="scope">
                    <span class="aaa">{{scope.row.link}}</span>
                </template>    
            </el-table-column>
            <el-table-column align="center" label="是否有中间页">
                <template slot-scope="scope">
                    <span>{{scope.row.centerPage===0?'否': '是'}}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNum" 
            :page-sizes="[10, 20, 50, 100]"
            :page-size="listQuery.pageSize" background
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
            style="text-align: center"
        ></el-pagination>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="btnDisabled" @click="chooseGoods">确定</el-button>
            <el-button @click="close">取消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { tbActivityList } from '@/api/bihuasuan/taobaoActivity'
    export default {
        name: 'taobaoActivity',
        props: {
            check: {
                type: Array,
                default() {
                    return [];
                }
            },
            count: {
                type: Number,
                default: -1
            }
        },
        data() {
            return {
                list: [],
                listQuery: {
                    activityName: '',
                    pageNum:1,
                    pageSize:10,
                },
                showGoodsPop: true,
                boxCheck: this.check,
                itemsCheck: this.check.length ? this.check.map(item => { return { id: item.id }}): [],
                itemsRadio: this.check.length ? {id: this.check[0].id} : {},
                // boxRadio: this.check[0],
                listLoading: false,
                btnDisabled: false,
                listTotal:0
            }
        },
        created() {
            // console.log(this.boxCheck)
            this.getList()
            // if (this.count === 1 && this.check.length > 0) {
            //     this.boxRadio = this.check[0];
            // }
        },
        methods: {
            // 获取列表
            getList() {
                this.listLoading = true
                tbActivityList(this.listQuery).then((res) =>{
                    this.list = res.data.list
                    this.listTotal = res.data.total
                    this.listLoading = false;
                }).catch(err=>{
                    console.log(err)
                    this.list = []
                    this.listTotal = 0;
                    this.listLoading = false;
                })
            },
            setValue(checked, row){
                if(checked){
                    if(this.count ===1 ){
                    this.boxCheck = [row]
                    } else {
                    this.boxCheck.push({ ...row });
                    }
                } else {
                    let index = this.itemsCheck.findIndex(item => row.id === item.id);
                    this.boxCheck.splice(index, 1);
                }
            },
            // 单击行
            clickRow(row) {
            if (this.count === 1) {
                this.itemsRadio = { id: row.id };
                this.boxCheck = [row];
            } else {
                let index = this.itemsCheck.findIndex(item => row.id === item.id);
                if (index < 0){
                this.itemsCheck.push({ id: row.id });
                this.boxCheck.push({ ...row });
                } else {
                this.itemsCheck.splice(index, 1);
                this.boxCheck.splice(index, 1);
                }
            }
            },
            // 通过tag删除
            delGoods(index) {
                this.boxCheck.splice(index, 1)  
            },
            // 选择商品
            chooseGoods() {
                // if(this.count === 1){
                //     this.boxCheck = [this.boxRadio]
                // }
                if(this.boxCheck.length < 1){
                    return this.$message.error('请选择活动');
                }
                if(this.count > 0 && this.boxCheck.length > this.count){
                    return this.$message.error(`最多选择${this.count}个活动`);
                }
                console.log(this.boxCheck)
                this.$emit('choose', this.boxCheck);
                this.close();
            },
            // 关闭弹窗
            close() {
                this.showGoodsPop = false
                document.body.removeChild(this.$el);
            },
            // 搜索
            search() {
                this.listQuery.pageNum = 1
                this.getList();
            },
            //切换每页条数
            handleSizeChange(value) {
            this.listQuery.pageSize = value
            this.getList()
            },
            // 切换分页
            handleCurrentChange(page) {
            this.listQuery.pageNum = page
            this.getList()
            }
        }
    }
</script>
<style scoped>
.aaa{
    height: 120px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
}
</style>